package com.social.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.fastjson.JSON;
import com.social.pojo.Permission;
import com.social.pojo.RegistrationOfCases;
import com.social.pojo.Role;
import com.social.pojo.User;
import com.social.service.UserService;
import com.social.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据库管理
 */
@Controller
@RequestMapping("DataBase")
public class DataBaseController {
    /**
     * User 业务层
     */
    @Autowired
    private UserService userService;

    /**
     * 批量导出
     *
     * @param map 集合
     * @return controller
     */
    @RequestMapping("bulkExport")
    public String bulkExport(ModelMap map) {
        List<User> users = userService.selectAlls();
        ExportParams exportParams1 = new ExportParams("用户管理", "所有用户", ExcelType.XSSF);
        //导出的集合
        map.put(NormalExcelConstants.DATA_LIST, users);
        map.put(NormalExcelConstants.CLASS, User.class);
        map.put(NormalExcelConstants.PARAMS, exportParams1);
        map.put(NormalExcelConstants.FILE_NAME, "用户信息");
        return "easypoiExcelView";
    }

    /**
     * 批量导入
     *
     * @param file    导入文件
     * @param session 作用域
     * @return 添加或修改的状态
     * @throws Exception 文件找不到
     */
    @RequestMapping(value = "bulkImport",produces = "text/application;charset=utf-8")
    @ResponseBody
    public String bulkImport(MultipartFile file, HttpSession session){
        //操作状态
        int status = 0;
        try {
            //获取文件后缀名
            String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            //获取路径
            File files = new File(session.getServletContext().getRealPath("/importFile/"));
            //判断文件夹是否存在
            if (!files.exists()) {
                files.mkdirs();
            }
            //随机数
            double v = Math.random() * 100;
            //时间戳
            String yyyyMMddHH = new SimpleDateFormat("yyyyMMddHH").format(new Date());
            //设置文件上传路径并将文件名设置进去
            file.transferTo(new File(files + "/" + (int) v + yyyyMMddHH + fileName));
            //创建导入对象
            ImportParams params = new ImportParams();
            params.setTitleRows(1); //表格标题行数,默认0
            params.setHeadRows(1);  //表头行数,默认1

            //获取导入数据
            List<User> teachers = ExcelImportUtil.importExcel(new FileInputStream(new File(files + "/" + (int) v + yyyyMMddHH + fileName)), User.class, params);
            for (User teacher : teachers) {
                User user = userService.queryByUsername(teacher.getUsername());
                if (user == null) {
                    status = userService.insertSelective(teacher);
                } else {
                    status = userService.updateByPrimaryKeySelective(teacher);
                }
            }
        } catch (Exception e) {
            JsonUtil jsonUtil = new JsonUtil();
            jsonUtil.setCode("请选择正确的文件");
            return JSON.toJSONString(jsonUtil.getCode());
        }
        //返回状态
        JsonUtil jsonUtil = new JsonUtil();
        //设置状态
        Boolean restates = jsonUtil.getRestates(status);
        return JSON.toJSONString(restates);
    }
}
